

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using JTC.MiniCommerce.Core;
using JTC.MiniCommerce.Core.Configuration;
using JTC.MiniCommerce.Localization;
using JTC.MiniCommerce.Store;

namespace JTC.MiniCommerce.Web.admin {
  public partial class mailconfiguration : System.Web.UI.Page {

    #region Member Variables

    private MailSettings mailSettings;

    #endregion

    #region Page Events

    /// <summary>
    /// Handles the Load event of the Page control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e) {
      try {
        SetMailSettingsProperties();
        mailSettings = MailSettings.Load();
        if (!Page.IsPostBack) {
          if (mailSettings != null) {
            txtFrom.Text = mailSettings.From;
            txtHost.Text = mailSettings.Host;
            chkRequireAuthentication.Checked = mailSettings.RequireAuthentication;
            txtUserName.Text = mailSettings.UserName;
            txtPassword.Text = mailSettings.Password;
            chkRequireSsl.Checked = mailSettings.RequireSsl;
            txtPort.Text = mailSettings.Port.ToString();
          }
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(mailconfiguration).Name + ".Page_Load", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    /// <summary>
    /// Handles the Click event of the btnSave control.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
    protected void btnSave_Click(object sender, EventArgs e) {
      try {
        if (mailSettings == null) {
          mailSettings = new MailSettings();
        }
        mailSettings.From = txtFrom.Text;
        mailSettings.Host = txtHost.Text;
        mailSettings.RequireAuthentication = chkRequireAuthentication.Checked;
        mailSettings.UserName = txtUserName.Text;
        mailSettings.Password = txtPassword.Text;
        int port = 25;
        int.TryParse(txtPort.Text, out port);
        mailSettings.RequireSsl = chkRequireSsl.Checked;
        mailSettings.Port = port;

        DatabaseConfigurationProvider databaseConfigurationProvider = new DatabaseConfigurationProvider();
        int id = databaseConfigurationProvider.SaveConfiguration(MailSettings.SECTION_NAME, mailSettings, WebUtility.GetUserName());
        if (id > 0) {
          Master.MessageCenter.DisplaySuccessMessage(LocalizationUtility.GetText("lblMailSettingsSaved"));
        }
        else {
          Master.MessageCenter.DisplayFailureMessage(LocalizationUtility.GetText("lblMailSettingsNotSaved"));
        }
      }
      catch (Exception ex) {
        Logger.Error(typeof(mailconfiguration).Name + ".btnSave_Click", ex);
        Master.MessageCenter.DisplayCriticalMessage(LocalizationUtility.GetCriticalMessageText(ex.Message));
      }
    }

    #endregion

    #region Methods

    #region Private

    /// <summary>
    /// Sets the mail settings properties.
    /// </summary>
    private void SetMailSettingsProperties() {
      LocalizationUtility.AddLabelText(lblMailConfiguration);
      LocalizationUtility.AddPanelText(pnlMailSettings);
      LocalizationUtility.AddLabelText(lblFrom);
      LocalizationUtility.AddLabelText(lblHost);
      LocalizationUtility.AddLabelText(lblUserName);
      LocalizationUtility.AddLabelText(lblPassword);
      LocalizationUtility.AddLabelText(lblPort);
      LocalizationUtility.AddButtonText(btnSave);
      this.Title = LocalizationUtility.GetText("titleMailSettings");
      LocalizationUtility.AddHoverHelp(hlMailSettingsFromHelp);
      LocalizationUtility.AddHoverHelp(hlMailSettingsHostHelp);
      LocalizationUtility.AddHoverHelp(hlMailSettingsUserNameHelp);
      LocalizationUtility.AddHoverHelp(hlMailSettingsPasswordHelp);
      LocalizationUtility.AddHoverHelp(hlMailSettingsPortHelp);
      LocalizationUtility.AddLabelText(lblRequireAuthentication);
      LocalizationUtility.AddLabelText(lblRequireSsl);
      LocalizationUtility.AddHoverHelp(hlRequireAuthenticationHelp);
      LocalizationUtility.AddHoverHelp(hlMailRequireSslHelp);
    }

    #endregion

    #endregion

  }
}
